# -*- makefile -*-
# vi:se ft=make:

###############################################################################

include $(srcdir)/Modules.generic

SUBSYSTEMS  += LIBUART

PREPROCESS_PARTS 	+= $(CONFIG_XARCH) $(CONFIG_ABI) libuart
PREPROCESS_PARTS	+= recover_jmpbuf
PREPROCESS_PARTS-$(CONFIG_BIT32) += 32bit mips32
PREPROCESS_PARTS-$(CONFIG_BIT64) += 64bit mips64
BITS-$(CONFIG_BIT32) = 32
BITS-$(CONFIG_BIT64) = 64
BITS = $(BITS-y)

PREPROCESS_PARTS-$(CONFIG_CPU_VIRT)          += mips_vz
PREPROCESS_PARTS-$(CONFIG_CPU_MIPSR6)        += mips_r6
PREPROCESS_PARTS-$(CONFIG_PAGE_SIZE_16KB)    += pagesize_16k
PREPROCESS_PARTS-$(CONFIG_PAGE_SIZE_4KB)     += pagesize_4k
PREPROCESS_PARTS-$(CONFIG_SERIAL)            += force_vkey
PREPROCESS_PARTS-$(CONFIG_MP)                += need_xcpu_tlb_flush
PREPROCESS_PARTS-$(CONFIG_JDB)               += debug log
PREPROCESS_PARTS-$(CONFIG_WEAK_ORDERING)     += weak_ordering
PREPROCESS_PARTS-$(CONFIG_LIGHTWEIGHT_BARRIERS) += mips_lw_barriers
PREPROCESS_PARTS	+= $(PREPROCESS_PARTS-y)

#
# TYPES subsystem
#
PRIVATE_INCDIR += types/mips/$(BITS) types/mips types

#
# UART subsystem
#
VPATH_LIBUART  := $(srcdir)/lib/uart
PRIVATE_INCDIR += lib/uart

#
# DRIVERS Subsystem
#
DRIVERS			:= libdrivers.a libgluedriverslibc.a
VPATH			+= drivers/mips/$(BITS) drivers/mips drivers
PRIVATE_INCDIR		+= drivers/mips/$(BITS) drivers/mips drivers
INTERFACES_DRIVERS	:= mux_console console mem reset uart filter_console \
			   processor delayloop io mmu
INTERFACES_DRIVERS	+= $(if $(CONFIG_UART_CHECKSUM),stream_crc32)
mem_IMPL		:= mem mem-mips
uart_IMPL		:= uart uart-libuart
processor_IMPL		:= processor processor-mips
CXXSRC_DRIVERS		:= glue_libc.cc
#NOOPT			+= $(patsubst %.o, %, $(OBJ_DRIVERS))

#
# JABI Subsystem
#
JABI		:= libjabi.a
VPATH		+= jabi/mips/$(BITS) jabi
INTERFACES_JABI	:= jdb_ktrace jdb_obj_info


#
# ABI Subsystem
#
l4_types_IMPL	+= l4_types-mips

#
# KERNEL subsystem
#
KERNEL 		:= fiasco
KERNEL_EXTRA 	:= Symbols kernel.mips.lds fiasco.debug
VPATH 		+= kern/mips/$(BITS) kern/mips kern
VPATH		+= jdb/mips/$(BITS) jdb/mips jdb
PRIVATE_INCDIR 	+= kern/mips/$(BITS) kern/mips kern

INTERFACES_KERNEL +=	boot_uart_init cp0_status cm \
			mips_cpu_irqs irq_chip_generic \
			alternatives \
			cpu_p5600

#missing: jdb_extensions

INTERFACES_KERNEL-$(CONFIG_SERIAL) += uart_console
INTERFACES_KERNEL-$(CONFIG_CPU_VIRT) += vz

INTERFACES_KERNEL += $(INTERFACES_KERNEL-y)

alternatives_IMPL	:= alternatives-mips
bitmap_linux_IMPL	:= bitmap_linux
clock_IMPL		:= clock
config_IMPL		:= config config-mips
context_IMPL		:= context context-mips context-vcpu
continuation_IMPL	:= continuation-mips
cp0_status_IMPL         := cp0_status
cpu_IMPL		:= cpu cpu-mips
cpu_lock_IMPL		:= cpu_lock cpu_lock-generic
entry_frame_IMPL	:= entry_frame-mips
fpu_IMPL		:= fpu fpu-mips
ipi_IMPL		:= ipi ipi-mips
kdb_ke_IMPL		+= kdb_ke-mips
kernel_task_IMPL	:= kernel_task
kernel_thread_IMPL	:= kernel_thread kernel_thread-mips
kernel_uart_IMPL  	:= kernel_uart kernel_uart-libuart
kmem_IMPL		:= kmem kmem-mips
kmem_alloc_IMPL		:= kmem_alloc kmem_alloc-mips
kmem_mmio_IMPL		:= kmem_mmio kmem_mmio-mips
map_util_IMPL		:= map_util map_util-mem map_util-objs
mem_layout_IMPL		:= mem_layout mem_layout-mips$(BITS)
mem_unit_IMPL		:= mem_unit-mips
mem_space_IMPL		:= mem_space mem_space-mips
obj_space_IMPL		:= obj_space obj_space-phys
paging_IMPL		:= paging paging-mips
perf_cnt_IMPL		:= perf_cnt perf_cnt-mips
platform_control_IMPL	+= platform_control-mips
cm_IMPL			:= cm
cpc_IMPL		:= cpc
pic_IMPL		:= pic
sched_context_IMPL	:= sched_context-wfq sched_context-fixed_prio \
			   sched_context-fp_wfq sched_context
space_IMPL		:= space
spin_lock_IMPL		:= spin_lock spin_lock-mips
startup_IMPL		:= startup startup-mips
sys_call_page_IMPL	:= sys_call_page
task_IMPL		:= task task-mips
timer_IMPL		?= timer timer-mips
timer_tick_IMPL         := timer_tick
thread_IMPL		:= thread thread-log thread-pagefault \
			   thread-mips thread-ipc thread-jdb \
			   thread-vcpu
vmem_alloc_IMPL		:= vmem_alloc
tb_entry_IMPL		:= tb_entry tb_entry-mips


ifeq ("$(CONFIG_JDB)","y")
JDB			:= jdb_compound.o
SUBSYSTEMS		+= JDB

CXXSRC_JDB := tb_entry_output.cc

jdb_IMPL		+= jdb-mips
jdb_tcb_IMPL		+= jdb_tcb-mips
jdb_entry_frame_IMPL	:= jdb_entry_frame-mips
jdb_trace_set_IMPL	+= jdb_trace_set jdb_trace_set-mips
jdb_kern_info_IMPL	:= jdb_kern_info
jdb_tbuf_init_IMPL	:= jdb_tbuf_init jdb_tbuf_init-mips
jdb_ptab_IMPL		:= jdb_ptab jdb_ptab-mips
thread_IMPL		+= thread-debug
INTERFACES_JDB		+= jdb_log jdb_trace_set jdb_ptab jdb_mips_tlb \
			   jdb_console_buffer
INTERFACES_JDB		+= $(INTERFACES_JDB-y)
endif

CXXSRC_KERNEL		:= kernel_panic.cc libc_backend_lock.cc
ASSRC_KERNEL		:= exception.S kip-time.S
#ASSRC_KERNEL-$(CONFIG_MP) += tramp-mp.S
ASSRC_KERNEL              += $(ASSRC_KERNEL-y)

NOOPT			+= $(filter jdb%,\
			     $(foreach in,$(INTERFACES_KERNEL), \
			       $(if $($(in)_IMPL),$($(in)_IMPL),$(in))))
NOOPT			+= tb_entry #tb_entry_output

#
# CRT0 subsystem
#
CRT0 		:= crt0.o
ASSRC_CRT0	:= crt0.S


#
# CXXLIB Subsystem
# 
CXXLIB 			:= libcxx.a
VPATH			+= lib/cxxlib
CXXSRC_CXXLIB 		:= paranoia.cc

#
# LIBK subsystem
#
LIBK 		:= libk.a
VPATH          	+= lib/libk/mips/$(BITS) lib/libk/mips lib/libk lib/libk/quad
PRIVATE_INCDIR 	+= lib/libk/mips/$(BITS) lib/libk/mips lib/libk

INTERFACES_LIBK	:= atomic lock_guard std_macros
CSRC_LIBK	+= umoddi3.c ashldi3.c udivdi3.c qdivrem.c ucmpdi2.c
CXXSRC_LIBK     += construction.cc
atomic_IMPL     := atomic atomic-mips

#
# SIMPLEMALLOC
#
SIMPLEMALLOC     := libsimple_malloc.a
VPATH             += lib/simple_malloc
PRIVATE_INCDIR    += lib/simple_malloc
CXXSRC_SIMPLEMALLOC = simple_malloc.cc

#
# LIBDISASM subsystem (only for Jdb)
#
ifeq ("$(CONFIG_JDB_DISASM)","y")
  # $(srcdir)/lib/disasm may be removed
  ifeq ($(wildcard $(srcdir)/lib/disasm),)
    $(error $(srcdir)/lib/disasm is missing, disable CONFIG_JDB_DISASM)
  endif
  SUBSYSTEMS          += LIBDISASM
  KERNEL_EXTRA_LIBS   += $(LIBDISASM)
  PREPROCESS_PARTS    += jdb_disasm

  SUBSYSTEMS          += SIMPLEMALLOC
  KERNEL_EXTRA_LIBS   += $(SIMPLEMALLOC)

  LIBDISASM           := libdisasm.a
  VPATH               += lib/disasm lib/disasm/capstone \
			 lib/disasm/capstone/arch/Mips
  PRIVATE_INCDIR      += lib/disasm lib/disasm/capstone/include
  CXXSRC_LIBDISASM    := disasm.cc
  CSRC_LIBDISASM      := cs.c utils.c \
                         MCInst.c MCInstrDesc.c MCRegisterInfo.c SStream.c \
                         MipsModule.c MipsInstPrinter.c \
                         MipsDisassembler.c MipsMapping.c
  CPPFLAGS            += -DCAPSTONE_HAS_MIPS
  CFLAGS_MipsInstPrinter  = -Wno-unused-parameter -Wno-bad-function-cast
  CFLAGS_MipsMapping      = -Wno-unused-parameter
  CFLAGS_MipsDisassembler = -Wno-unused-parameter
endif

#
# VERSION subsystem
#
VERSION		:= version.h

TCBOFFSET	:= tcboffset.h
CXXSRC_TCBOFFSET	:= tcboffset.cc dump_tcboffsets.cc

#
# BSP subsystem
#
BSP_NAME         := $(patsubst "%",%,$(CONFIG_BSP_NAME))
MODULES_FILE_BSP := $(srcdir)/kern/mips/bsp/$(BSP_NAME)/Modules
PRIVATE_INCDIR 	 += kern/mips/bsp/$(BSP_NAME)
ifeq ($(wildcard $(MODULES_FILE_BSP)),)
  $(error No BSP name defined or no BSP Modules file available)
endif

include $(MODULES_FILE_BSP)
VPATH += kern/mips/bsp/$(BSP_NAME) kern/mips/bsp

ifeq ("$(filter LIBUART, $(SUBSYSTEMS))","LIBUART")
  LIBUART			:= uart/libuart.a
endif

CONFIG_KERNEL_LOAD_ADDR-32 := 0x80010000
CONFIG_KERNEL_LOAD_ADDR-64 := 0xffffffff80010000
CONFIG_KERNEL_LOAD_ADDR := $(CONFIG_KERNEL_LOAD_ADDR-$(BITS))

MODULES_FILES += $(MODULES_FILE) $(MODULES_FILE_BSP)
